<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:角色管理
// +----------------------------------------------------------------------


namespace app\admin\controller;
 use app\BackendController;
use app\admin\model\Role as cModel;
use think\facade\Cache;
use support\Request;
use think\facade\Db;
use support\Util\InitTree;

class RolesController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    function list() {
        return view("roles/list", []);
    }

    public function listData(Request $request)
    {
        $limit                                  = $request->get('limit', 10);
        $w                                      = [];
        $request->get('r_name') && $w['r_name'] = array('like', '%' . $request->get('r_name') . '%');
        $ls         = cModel::with('roleUser')->where($w)->paginate(['list_rows' => $limit, 'request' => $request->all()]);
        
        $ls = $ls->toArray();      
        array_walk($ls['data'], function(&$v){
            $v['role_user_count'] = count($v['roleUser']);
        });            
        return json(['code' => 0, 'msg' => '', 'count' => $ls['total'], 'data' => $ls['data']]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $menus = $this->getMenus();
        return view("roles/create", compact('menus'));
    }

    private function getMenus()
    {
        $menus = Db::name("role_access")->column("ac_id as id,ac_name as name,ac_pid as parentid,ac_url", "ac_id");

        $tree = new InitTree;
        $tree->set_tree($menus);
        $ls = $tree->get_childs(0);
        $ls = array_map(function ($v) {
            $v['path_str'] = str_replace(',', '_', $v['path']);
            return $v;
        }, $ls);              
        return $ls;
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post = $request->post();
        if(!$post['r_auth']){
            return json(['code' => 0, 'msg' => '请选择权限']);
        }
        $t    = new cModel();
        $t->save($post);
        return json(['code' => 0, 'msg' => '保存成功']);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id=  $request->get("id");
        $menus = $this->getMenus();
        $data  = cModel::find($id)->toArray();                        
        return view('roles/edit', compact('data', 'menus'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id=  $request->get("id");
        $post = $request->post();
        $t    = new cModel();
        $t->where('r_id', $id)->save($post);

        return json(['code' => 0, 'msg' => '保存成功']);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $r_id = $request->input("r_id");
       
        $ex = Db::name("role_user")->where('r_id','in',$r_id)->count();        
        if($ex){
           
            $r = ['code' => 0, 'msg' => '角色下存在用户，请先取消关联'];
        }else{

            cModel::destroy($r_id);
            $r = ['code' => 0, 'msg' => '删除成功'];
            
        } 
        return json($r);         
    

    }
}
